package com.bdqn.Demo.controller;

import com.bdqn.Demo.pojo.SysUser;
import com.bdqn.Demo.serivce.SysUserService;
import com.bdqn.Demo.utils.Constants;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class LoginController {
    private static final Logger logger=Logger.getLogger(LoginController.class);
    @Resource
    private SysUserService sysUserService;

    @RequestMapping("/toLogin")
    public String login(){
        return "login";
    }

    @PostMapping("/login")
    public String doLogin(@RequestParam String account, @RequestParam String password, HttpServletRequest request, HttpSession session)throws Exception{
        SysUser sysUser=sysUserService.login(account, password);
        if (null!=sysUser) {
            session.setAttribute(Constants.USER_SESSION,sysUser);
            return "redirect:/sys/toMain";
        }else{
            request.setAttribute("error","用户或密码错误");
            return "login";
        }
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute(Constants.USER_SESSION);
        return "login";
    }

    @RequestMapping("/sys/toMain")
    public String toMain(){
        return "frame";
    }
}
